import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { resolve } from 'path'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
      imports: [
        'vue', 
        'vue-router'
      ],
      dts: './src/auto-imports.d.ts', // 指定生成类型声明文件的位置
    }),
    Components({
      resolvers: [ElementPlusResolver({
        importStyle: 'css', // 使用 CSS 样式而不是 SASS
      })],
      dts: true, // 生成类型声明文件
    }),
  ],
  resolve: {
    alias: {
      '@': resolve(__dirname, 'src')
    }
  },
  server: {
    proxy: {
      '/api': {
        target: 'http://47.111.160.118:8080',
        changeOrigin: true,
        // 确保登录响应里的 Set-Cookie 域名被重写为 localhost，便于在前端 document.cookie 可读
        cookieDomainRewrite: 'localhost',
        // 保证路径为根路径
        cookiePathRewrite: '/',
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  }
})
